package com.yqg.manage.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.manage.common.interceptor.SysLog;
import com.yqg.manage.entity.Term;
import com.yqg.manage.mapper.LawsMapper;
import com.yqg.manage.service.ITermService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.manage.common.Result;

import com.yqg.manage.service.ILawsService;
import com.yqg.manage.entity.Laws;

import org.springframework.web.bind.annotation.RestController;

/**
 * LawsController 前端控制器
 *
 * @author zxf
 * @since 2025-07-26
 */
@RestController
@RequestMapping("/laws")
public class LawsController {

    @Resource
    private ILawsService lawsService;

    @Resource
    private LawsMapper lawsMapper;

    @Resource
    private ITermService termService;


    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @SysLog("新增或者更新法律")
    public Result save(@RequestBody Laws laws) {
        LambdaQueryWrapper<Laws> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Laws::getName, laws.getName())
                .eq(Laws::getType, laws.getType())
                .eq(Laws::getIsDelete, "0");

        boolean exists = lawsService.count(lambdaQueryWrapper) > 0;

        if (exists && laws.getId() == null) {
            // 记录已存在，不执行保存
            return Result.error("该法律已存在，无需保存");
        } else {
            // 记录不存在时执行保存操作
            boolean result = lawsService.saveOrUpdate(laws);
            return Result.success(result);
        }
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    @SysLog("删除法律")
    public Result delete(@PathVariable Integer id) {
        LambdaQueryWrapper<Term> termWrapper = new LambdaQueryWrapper<>();
        termWrapper.eq(Term::getLawsId,id)
                .eq(Term::getIsDelete,"0");
        List<Term> termList = termService.list(termWrapper);
//        判断是否有未删除的条款
        if (!termList.isEmpty()) {
            return Result.error("该法律下还有"+termList.size()+"条未删除的条款");
        }else {
            Laws laws = lawsService.getById(id);
            laws.setIsDelete("1");
            return Result.success(lawsService.saveOrUpdate(laws));
        }
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Laws> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<Laws> list = lawsService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(lawsService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(required = false) String type,
                           @RequestParam(required = false) String name) {
        QueryWrapper<Laws> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("type");
        if (type != null) {
            queryWrapper.like("type", type);
        }
        if (name != null) {
            queryWrapper.like("name", name);
        }
        return Result.success(lawsService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    //    获取法律类型
    @GetMapping("/getLawsType")
    public Result getLawsType() {
        List<String> list = lawsMapper.getALLType();
        return Result.success(list);
    }


}

